<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <router-link to="/home">主页</router-link>
        <router-link to="/product/111">产品详情1</router-link>
        <router-link to="/product/222">产品详情2</router-link>
        <!-- 路由出口 -->
        <router-view></router-view>
    </div>
</body>
<script src="./node_modules/vue/dist/vue.js"></script>
<script src="./node_modules/vue-router/dist/vue-router.js"></script>
<script>
    let routes=[
        {
            path:"/home",
            
            // 2、/home路由独享的导航守卫
            beforeEach:(to,from,next)=>{
                console.log("执行 /home路由独享的导航守卫");
                next();
            },
            component:()=>Promise.resolve({
                template:`<h1>主页内容</h1>`,
                // 组件内的导航守卫
                // 路由进入前
                beforeRouteEnter(to,from,next) {
                    console.log("进入到主页的路由");
                    // console.log(this);// undefined 现在还不可以访问到实例
                    next();
                },
                // 路由更新时 从product/111=>product/222
                beforeRouteUpdate(to,from,next){
                    console.log("主页路由更新时");
                    // console.log(this);// this=>产品详情组件
                    next();
                },
                // 路由离开时 
                beforeRouteLeave(to,from,next){
                    console.log("离开/home路由");
                    // console.log("现在的路由：",from);
                    // console.log("前往的路由：",to);
                    // console.log(this);// this=>产品详情组件
                    next();
                }
            }),
        },
        {
            path:"/product/:productId",
            component:()=>import("./Product.js"),
            // 2、/product路由独享的导航守卫
            beforeEach:(to,from,next)=>{
                console.log("执行 /product路由独享的导航守卫");
                next();
            }
        }
    ]

    let router=new VueRouter({
        routes
    })

    // 1、全局前置导航守卫
    router.beforeEach((to,from,next)=>{
        console.log("执行 全局的 前置的导航守卫");
        next();
    })
    let vm=new Vue({
        el:"#app",
        router,
    })


</script>
</html>